/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: 北京银杉金服科技有限公司
 * 系统版本: 1.0.0
 * 开发人员: zhangfb
 * 开发时间: 2018/9/23 23:53
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.hyacinth.service.impl;

import com.hyacinth.constant.ErrorCodeEnum;
import com.hyacinth.constant.LogTypeEnum;
import com.hyacinth.dto.OperationLogDto;
import com.hyacinth.entity.InterfaceLog;
import com.hyacinth.entity.LoginLog;
import com.hyacinth.exception.BusinessException;
import com.hyacinth.repository.jpa.InterfaceLogRepository;
import com.hyacinth.repository.jpa.LoginLogRepository;
import com.hyacinth.service.LogService;
import com.hyacinth.util.RequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zhangfb
 * @version 1.0.0.1
 * @since JDK 1.8
 */
@Slf4j
@Service
@Transactional
public class LogServiceImpl implements LogService {

    private final LoginLogRepository loginLogRepository;
    private final InterfaceLogRepository interfaceLogRepository;

    @Autowired
    public LogServiceImpl(LoginLogRepository loginLogRepository,
                          InterfaceLogRepository interfaceLogRepository) {
        this.loginLogRepository = loginLogRepository;
        this.interfaceLogRepository = interfaceLogRepository;
    }

    @Override
    public void save(OperationLogDto logDto) {
        // 日志类型
        String logType = logDto.getLogType();
        LogTypeEnum logTypeEnum = LogTypeEnum.getEnum(logType);
        switch (logTypeEnum) {
            case LOGIN_LOG:
                saveLoginLog(logDto);
                break;
            case EXCEPTION_LOG:
                saveExceptionLog(logDto);
                break;
            case INTERFACE_LOG:
                saveInterfaceLog(logDto);
                break;
            case SYSTEM_LOG:
                saveSystemLog(logDto);
                break;
            default:
                throw new BusinessException(ErrorCodeEnum.GL99990008);
        }
    }

    /**
     * 创建登陆日志
     * @param logDto 日志对象
     */
    private void saveLoginLog(OperationLogDto logDto){
        LoginLog loginLog = new LoginLog();
        loginLog.setUserCode(logDto.getCreatorCode());
        loginLog.setUserName(logDto.getCreator());
        loginLog.setRoleCodes(logDto.getAuthCode());
        loginLog.setAppType(logDto.getOs());
        loginLog.setDeviceNo(logDto.getMac());
        loginLog.setCreateTime(new Date());
        loginLog.setIpAddress(logDto.getIp());
        loginLog.setOpType(RequestUtil.loginOpType(logDto.getRequestUrl()));
        loginLog.setLoginDelayTime(logDto.getDelayTime());
        loginLogRepository.save(loginLog);
    }

    /**
     * 创建接口日志
     * @param logDto 日志对象
     */
    private void saveInterfaceLog(OperationLogDto logDto){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        InterfaceLog interfaceLog = new InterfaceLog();
        interfaceLog.setAppType(logDto.getOs());
        interfaceLog.setBegTime(sdf.format(logDto.getStartTime()));
        interfaceLog.setEndTime(sdf.format(logDto.getEndTime()));
        interfaceLog.setDelayTime(logDto.getDelayTime());
        interfaceLog.setReqUrl(logDto.getRequestUrl());
        interfaceLog.setMethod(logDto.getMethodName());
        interfaceLog.setUserCode(logDto.getCreator());
        interfaceLog.setReqInfo(logDto.getRequestData());
        interfaceLog.setRespInfo(logDto.getResponseData());
        interfaceLog.setRetCode(logDto.getRetCode());
        interfaceLog.setRetMsg(logDto.getRetMsg());
        interfaceLogRepository.save(interfaceLog);
    }

    /**
     * 创建异常日志
     * @param logDto 日志对象
     */
    private void saveExceptionLog(OperationLogDto logDto){

    }

    /**
     * 创建系统日志
     * @param logDto 日志对象
     */
    private void saveSystemLog(OperationLogDto logDto){

    }

}
